package com.dinadi.yidi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.CreativeHotspotRequestEntity;
import com.dinadi.yidi.bean.ExhibitionreaRequestEntity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 12:53
 */

public class CreativeHotspotAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<CreativeHotspotRequestEntity> mDatas = new ArrayList<>();
    private View view;
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private CreativeHotspotAdapter.OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }


    public void addAllData(List<CreativeHotspotRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public CreativeHotspotAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_creative_hotspot_item, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new CreativeHotspotAdapter.ItemViewHolder(v);


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        CreativeHotspotAdapter.ItemViewHolder itemViewHolder = (CreativeHotspotAdapter.ItemViewHolder) holder;
        itemViewHolder.text_msg.setText(mDatas.get(position).getTitle());
        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);
            }
        }else{
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);

        }
        itemViewHolder.star_num.setText(mDatas.get(position).getStarNum());
        itemViewHolder.read_num.setText(mDatas.get(position).getReadNum());
        String id = mDatas.get(position).getSubjectArticleId();
        String hotId = mDatas.get(position).getHotId();
        holder.itemView.setTag(hotId);

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView text_msg, star_num, read_num;
        ImageView imageView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            text_msg = (TextView) itemView.findViewById(R.id.text_title);
            imageView = (ImageView) itemView.findViewById(R.id.cover_img);
            star_num = (TextView) itemView.findViewById(R.id.star_num);
            read_num = (TextView) itemView.findViewById(R.id.read_num);
        }


    }

    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(CreativeHotspotAdapter.OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

}

